package DAL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.ConnectionManager;
import entities.UserVote;

public class UserVoteDA {
	public UserVote[] selectUserVote(UserVote user)
	{
		boolean whereClause = false;
		String query = "SELECT * from UserVote  " ;
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(user.getUserID() != -1) 
		{
			if(whereClause)
				query += " AND UserID = " + user.getUserID() + "";
			else
				query += " WHERE UserID = " + user.getUserID() + "";
			whereClause = true;
		}
		if(user.getVotingID() != -1) 
		{
			if(whereClause)
				query += " AND VotingID = " + user.getVotingID() + "";
			else
				query += " WHERE VotingID = " + user.getVotingID() + "";
			whereClause = true;
		}
		if(user.isCanVoteSet()) 
		{
			if(whereClause)
				query += " AND CanVote = '" + user.isCanVote() + "'";
			else
				query += " WHERE CanVote = '" + user.isCanVote() + "'";
			whereClause = true;
		}
		
		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<UserVote> users = new ArrayList<UserVote>();

		
		try {
			while(rs.next())
			{
				UserVote tempUser= new UserVote();
				tempUser.setUserID(rs.getInt("userID"));
				tempUser.setAdminID(rs.getInt("adminID"));
				tempUser.setVotingID(rs.getInt("VotingID"));
				tempUser.setCanVote(rs.getBoolean("CanVote"));
				users.add(tempUser);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UserVote [] temp = new UserVote [0];
		return users.toArray(temp);

	}


	public boolean updateUserVote(UserVote user)
	{
		String query = "UPDATE UserVote SET " ;
		query += " CanVote = '" + user.isCanVote() + "'  ";
	
		
		query += " WHERE "  
		+ "  AdminID = " + user.getAdminID()
		+ " AND VotingID = " + user.getVotingID()
		+ " AND UserID = " + user.getUserID();
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertUserVote(UserVote user)
	{
		String query = "INSERT INTO UserVote (AdminID , UserID , VotingID , CanVote ) VALUES " +
				"(" +user.getAdminID() +  " , " + user.getUserID() + " , " + user.getVotingID() + " , '" + user.isCanVote() +"' )";
		
		ConnectionManager con = new ConnectionManager();
		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteUserVote(UserVote user)
	{
		boolean whereClause = false;
		String query = "DELETE FROM UserVote ";
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(user.getUserID() != -1) 
		{
			if(whereClause)
				query += " AND UserID = " + user.getUserID() + "";
			else
				query += " WHERE UserID = " + user.getUserID() + "";
			whereClause = true;
		}
		if(user.getVotingID() != -1) 
		{
			if(whereClause)
				query += " AND VotingID = " + user.getVotingID() + "";
			else
				query += " WHERE VotingID = " + user.getVotingID() + "";
			whereClause = true;
		}
		if(user.isCanVoteSet()) 
		{
			if(whereClause)
				query += " AND CanVote = '" + user.isCanVote() + "'";
			else
				query += " WHERE CanVote = '" + user.isCanVote() + "'";
			whereClause = true;
		}
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		UserVote u = new UserVote();

		u.setAdminID(2);
		u.setVotingID(5);
		u.setUserID(4);
		//u.setCanVote(true);
		
		UserVoteDA da = new UserVoteDA();
		
		System.out.println(da.updateUserVote(u));
	}
}
